<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
 *
 * functions.php template file
 *
 * (c) 2011 by Mike Walsh
 *
 * @author Mike Walsh <mike@walshcrew.com>
 * @package casl-soccer
 * @subpackage functions
 * @version $Revision$
 * @lastmodified $Author$
 * @lastmodifiedby $Date$
 *
 */

// Adds a home link to your menu
// http://codex.wordpress.org/Template_Tags/wp_page_menu
function childtheme_menu_args($args) {
    $args = array_merge($args, array(
        'show_home' => 'Home',
        'sort_column' => 'menu_order',
        'menu_class' => 'menu',
        'echo' => true
    )) ;
	return $args ;
}
add_filter('wp_page_menu_args','childtheme_menu_args');

// We're using the menu CSS from the Twenty Ten theme so this
// override strips the Superfish CSS class that Thematic adds.
function childtheme_nav_menu_args($args) {
    $args = array_merge($args, array(
        'menu_class' => ''
    )) ;
	return $args ;
}
add_filter('wp_nav_menu_args','childtheme_nav_menu_args');

//  Suppress loading SuperFish since we're not using Thematic menus
add_filter('thematic_use_superfish', '__return_false') ;

// Unleash the power of Thematic's dynamic classes
// 
define('THEMATIC_COMPATIBLE_BODY_CLASS', true);
define('THEMATIC_COMPATIBLE_POST_CLASS', true);

// Unleash the power of Thematic's page comments
//
define('THEMATIC_COMPATIBLE_COMMENT_HANDLING', true);

// Unleash the power of Thematic's comment form
//
define('THEMATIC_COMPATIBLE_COMMENT_FORM', true);

// Unleash the power of Thematic's feed link functions
//
define('THEMATIC_COMPATIBLE_FEEDLINKS', true);

//  Load CASL extensions
require_once(get_stylesheet_directory() . '/library/extensions/header-extensions.php') ;
require_once(get_stylesheet_directory() . '/library/extensions/sidebar-extensions.php') ;
require_once(get_stylesheet_directory() . '/library/extensions/widgets-extensions.php') ;
require_once(get_stylesheet_directory() . '/library/extensions/options-extensions.php') ;
require_once(get_stylesheet_directory() . '/library/extensions/post-type-extensions.php') ;

/**
 * Enable shortcodes in text widgets.
 */
add_filter('widget_text', 'do_shortcode');

//  Add Post Thumbnail Support
add_theme_support('menus') ;
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'twentyten' ),
	) );
//add_theme_support('post-thumbnails') ;
//set_post_thumbnail_size(540, 300, true) ;
//add_image_size('homepage-thumbnail', 300, 200, true) ;


if (function_exists('add_theme_support'))
{
	add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}

if (function_exists('add_image_size'))
{ 
	add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
	add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
	add_image_size( 'rosterpage-thumb', 75, 100, true ); //(cropped)
	add_image_size( 'rosterpage-headshot', 200, 300, true ); //(cropped)
}


function casl_add_post_thumb($title)
{
    return get_the_post_thumbnail(NULL, 'thumbnail') . $title;
}
add_filter('get_the_excerpt', 'casl_add_post_thumb');


/**
 * Build Page Category Meta Box to allow the user to select
 * a single category for the page.  This category and custom
 * field will be use to drive a custom loop on a page template.
 *
 */
function casl_page_category_mb()
{
    add_meta_box('casl-page-category-meta',
        'Page Category',
        'casl_page_category_meta', 'page', 'side', 'high') ;
}

add_action('add_meta_boxes', 'casl_page_category_mb') ;

/**
 * Build the Page Catgory form that will reside in the Meta Box.
 * Assigning a category to a page allows a custom category loop
 * to appear on a Page and just display the posts from that one
 * category.
 *
 */
function casl_page_category_meta($page)
{
    $msg = '<div class="misc-pub-section">Select a category to be used by the <b><i>Standard Width
        Category</i></b> page template.  Setting the category to <b><i>
        No Category Assigned</i></b> will remove the Page Category.</div>' ;

    //  Has a page category already been assigned?  If so, get it.
    $page_cat = get_post_meta($page->ID, '_casl_page_cat', true) ;

    //  Set the argument list to construct the categories drop down
    $args = array(
        'orderby' => 'name'
       ,'name' => 'casl_page_cat'
       ,'selected' => ($page_cat === "") ? 0 : $page_cat
       ,'show_option_none' => __('No Category Assigned')
       ,'hide_empty' => 0
    ) ;
    ?>
    <p>Category:
    <?php wp_dropdown_categories($args) ; ?>
    </p>
    <?php
    echo __($msg) ;
}
add_action('save_post', 'casl_save_page_category') ;

/**
 * Process the Page Category from the Meta Box.  If
 * no Page Category is assigned, then remove any value
 * that may have been set previously.
 *
 */
function casl_save_page_category($page_id)
{
    //  Verify meta data was set
    if (isset($_POST['casl_page_cat']))
    {
        $cat_id = intval($_POST['casl_page_cat']) ;

        //  Save the page category id or delete it

        if ($cat_id === -1)
            delete_post_meta($page_id, '_casl_page_cat') ;
        else
            update_post_meta($page_id, '_casl_page_cat',
                intval($_POST['casl_page_cat'])) ;
    }
}

/**
 * Add class names for each post based on their category names.
 *
 */
function casl_category_name_class($classes)
{
    global $post ;

    foreach((get_the_category($post->ID)) as $category)
        $classes[] = $category->category_nicename ;

    return $classes ;
}

/**
 * Return the category slug for a category id.
 *
 * @retunrn string - category slug text
 */
function casl_get_cat_slug($cat_id) {
	$cat_id = (int) $cat_id;
	$category = &get_category($cat_id);
	return $category->slug;
}

/**
 * If a page has been assigned a category, add a body class
 * based on the category slug so the page can be styled.
 *
 * @param mixed - existing classes
 * @return muxed - updated classes
 */
function casl_body_class($classes)
{
    global $post ;

    $cat_id = get_post_meta($post->ID, '_casl_page_cat', true) ;

    if ($cat_id !== "")
    {
        $cat_slug = casl_get_cat_slug($cat_id) ;
        $classes[] = 'page-category-' . $cat_slug ;
    }

    //  Check for the custom post type(s)
    
    if (get_post_type() == 'player')
        $classes[] = 'page-roster-player' ;

    return $classes ;
}
//  Hook into Thematic's body class hook
add_filter('thematic_body_class', 'casl_body_class') ;

// custom homepage loop
function casl_home_indexloop() {
  query_posts("posts_per_page=4");
  $counter = 1;
  if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
      <?php thematic_postheader();
      if ($counter == 1 && has_post_thumbnail() && !is_paged()) {
        //the_post_thumbnail('homepage-thumbnail');
      } ?>
      <div class="entry-content">
        <?php the_excerpt(); ?>
        <a href="<?php the_permalink(); ?>" class="more"><?php echo more_text() ?></a>
        <?php $counter++; ?>
      </div>
    </div><!-- .post -->
  <?php endwhile; else: ?>
    <h2>Bummer ...</h2>
    <p>There are no posts to show!</p>
  <?php endif;
  wp_reset_query();
}
?>
